import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_flux/flutter_flux.dart';
import 'package:freemusic/components/Player.dart';
import 'package:freemusic/page/search/SearchHistory.dart';
import 'package:freemusic/page/search/SearchResult.dart';
import 'package:freemusic/services/SongServices.dart';
import 'package:freemusic/stores/SongStores.dart';
import 'package:freemusic/utils/Util.dart';

class SearchPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return SearchState();
  }
}

class SearchState extends State<SearchPage> with StoreWatcherMixin<SearchPage> {
  SongStore _songStore;
  String keyword;

  @override
  void initState() {
    super.initState();
    _songStore = listenToStore(songStoreToken, (store) {
      this.setState(() {
        this.keyword = _songStore.keyword;
      });
    });
    //获取搜索历史
    SongServices.getInstace().loadKerwordHistory();
  }

  @override
  void dispose() {
    super.dispose();
    SongServices.getInstace().emptySongList();
  }

  void handleTextFieldOnChange(value) {
    this.setState(() {
      this.keyword = value;
    });
  }

  handleOnSearch() {
    String keyword = this.keyword;
    if (keyword == null || keyword.isEmpty) {
      return;
    }
    SongServices.getInstace().search(keyword);
    FocusScope.of(context).unfocus();
  }

  @override
  Widget build(BuildContext context) {
    Color backgroundColor = Util.backgroundColor;
    Color themeColor = Util.themeColor;
    SongStore _songStore = this._songStore;
    String keyword = this.keyword;

    return AnnotatedRegion(
      child: Scaffold(
        backgroundColor: backgroundColor,
        appBar: AppBar(
          backgroundColor: Color.fromRGBO(1, 1, 1, 0),
          elevation: 0,
          iconTheme: IconThemeData(color: themeColor),
          title: Container(
            height: 34,
            child: TextField(
              autofocus: true,
              decoration: InputDecoration(
                hintText: "输入你想听的歌曲名称",
                fillColor: Colors.white,
                filled: true,
                contentPadding: EdgeInsets.all(5.0),
                border: OutlineInputBorder(
                    borderRadius: BorderRadius.circular(16.0),
                    borderSide: BorderSide.none),
              ),
              onChanged: this.handleTextFieldOnChange,
            ),
          ),
          actions: <Widget>[
            IconButton(
              onPressed: this.handleOnSearch,
              icon: Icon(
                Icons.search,
                size: 32,
              ),
            )
          ],
        ),
        body: Listener(
          onPointerMove: (e) {
            FocusScope.of(context).unfocus();
          },
          child: ListView(
            padding: EdgeInsets.only(left: 8.0, right: 8.0),
            children: <Widget>[
              SearchHistoryWidget(
                keywords: _songStore.keywords,
                keyword: keyword,
              ),
              SearchResult(
                "QQ",
                Duration(seconds: 1),
                songs: _songStore.qqs,
                keyword: keyword,
              ),
              SearchResult(
                "KuGou",
                Duration(seconds: 1),
                songs: _songStore.kugous,
                keyword: keyword,
              ),
              SearchResult(
                "Netease",
                Duration(seconds: 1),
                songs: _songStore.neteases,
                keyword: keyword,
              ),
              SearchResult(
                "MiGu",
                Duration(seconds: 1),
                songs: _songStore.migus,
                keyword: keyword,
              )
            ],
          ),
        ),
        bottomNavigationBar: PlayerWidget(),
      ),
      value: SystemUiOverlayStyle.dark,
    );
  }
}
